﻿using HostAssistantBLL;
using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HostAssistant
{
    public partial class mssqlbak : U1Form
    {
        string id = "";
        string name = "";
        string dbname = "";
        string bakname = "";
        public mssqlbak(string _id)
        {
            id = _id;
            InitializeComponent();
        }
        private void addsites_Load(object sender, EventArgs e)
        {
            var SqlInfo = dbbll.GetSqlInfo(id);
            name = SqlInfo?.Mdbname ?? "";
            dbname = SqlInfo?.Mdbname ?? "";
        //   dbname = "0630";
            lblname.Text = SqlInfo?.Mdbname ?? "";
            bakname = lbltype.Text = SqlInfo?.Mdbname + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".bak";
            lblpath.Text = PathSetbll.GetPathSet()?.DbBakPath ?? "";
        }

        #region 返回
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 备份数据库
        void Addmssql()
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");

            var p = PathSetbll.GetPathSet();
            if (p == null || string.IsNullOrWhiteSpace(p?.DbBakPath))
            {
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("未设置备份路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
              string password = "";
            string port = "";
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mssql");
            string ip = mysql?.ip ?? "";
            port = mysql?.port ?? "";
            string user = mysql?.username ?? "";
            password = mysql?.userpwd ?? "";



            SqlDbHelper.Initialize("master", user, password);
            if (SqlDbHelper.ConnectionTest())
            {
                 
                string s = dbbll.EcreateTaskSchd_Mssql_Single(bakname, dbname, p.DbBakPath + @"\mssql\" + dbname + "\\");

                if (s != null)
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show(s, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("备份完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            else
            {
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("未能连接上数据库,请检查数据库账号密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
            private void simpleButton5_Click(object sender, EventArgs e)
        {
            try
            {
                OpHelper.CallWithTimeout(Addmssql, 600000);

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }



         
        }
        #endregion 
    }
}